<style>
    .w-e-text-container {
        height: 240px!important;
    }
</style>
<div>
    <div class="col-lg-12 col-xs-12">
        <h4 style="display: inline-block;"><span class="fa fa-hand-o-right"></span>
            {% if cid > 0 %}修改课程{% else %}添加课程{% endif %}
        </h4>
        <div style="padding: 15px 0;" class="pull-right">
            <button onclick="location.reload();" class="btn btn-warning btn-sm">返回列表</button>
        </div>
    </div>
    <div class="col-lg-12 col-xs-12" style="padding: 10px 40px 0;">
        <div class="panel panel-info bd-color-x">
            <div class="panel-heading bg-color-x">
                <span><span class="fa fa-plus-circle"></span>
                    {% if cid > 0 %}修改课程{% else %}添加课程{% endif %}
                </span>
            </div>
            <div class="panel-body">
                <form method="post" class="form-horizontal">
                    {% if cid > 0 %}
                        <input type="hidden" name="id" value="{{ cid }}">
                    {% endif %}
                    <input type="hidden" name="k_status" value="{{ clazz.k_status }}">
                    <input type="hidden" name="k_stu_num" value="{{ clazz.k_stu_num }}">
                    <table class="table">
                        <tr>
                            <td class="form-group">
                                <label for="password" class="control-label col-lg-2 col-xs-4">课程名称</label>
                                <div class="col-lg-10 col-sm-10">
                                    <input name="k_name" type="text" class="form-control"
                                           value="{% if cid > 0 %}{{ clazz.k_name }}{% endif %}"
                                           placeholder="2018软件开发第一次课">
                                </div>
                            </td>
                            <td class="form-group">
                                <label for="password" class="control-label text-left col-lg-2 col-xs-4">任课老师</label>
                                <div class="col-lg-10 col-sm-10">
                                    <input name="k_teacher" type="text" class="form-control"
                                           value="{% if cid > 0 %}{{ clazz.k_teacher }}{% endif %}"
                                           placeholder="耿老师">
                                </div>
                            </td>
                            <td class="form-group">
                                <label for="password" class="control-label text-left col-lg-2 col-xs-4">上课地点</label>
                                <div class="col-lg-10 col-sm-10">
                                    <select id="location" class="form-control">
                                        {% for local in locals %}
                                            {% if clazz.location.id == local.id %}
                                                <option data-id="{{ local.id }}" selected>{{ local.location_name }}</option>
                                            {% else %}
                                                <option data-id="{{ local.id }}">{{ local.location_name }}</option>
                                            {% endif %}
                                        {% endfor %}
                                    </select>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="form-group">
                                <label for="password" class="control-label col-lg-2 col-xs-4">上课时间</label>
                                <div class="col-lg-10 col-sm-10">
                                    <input id="start" name="k_time" type="text" class="laydate-icon form-control"
                                           value="{% if cid > 0 %}{{ clazz.k_time }}{% endif %}">
                                </div>
                            </td>
                            <td class="form-group">
                                <label for="password" class="control-label text-left col-lg-2 col-xs-4">作业期限</label>
                                <div class="col-lg-10 col-sm-10">
                                    <input id="end" name="k_work_endtime" type="text" class="laydate-icon form-control"
                                           value="{% if cid > 0 %}{{ clazz.k_work_endtime }}{% endif %}">
                                </div>
                            </td>
                            <td></td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                {% include 'admin/w-edit.html' %}
                                <input id="k_work" type="hidden" name="k_work">
                            </td>
                        </tr>
                    </table>
                    <div class="text-right">
                        {% if cid > 0 %}
                            <button id="xg" type="button" class="btn btn-success">修改</button>
                        {% else %}
{#                            <button type="button" onclick="f_reset();" class="btn btn-warning">重置</button>#}
                            <button id="add" type="button" class="btn btn-info">添加</button>
                        {% endif %}
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('#add').click(function (e) {
            if ($('[name="k_name"]').val().trim() === "") {
                alert("课程名不能为空!");
                return;
            }
            if ($('[name="k_teacher"]').val().trim() === "") {
                alert("任课老师不能为空!");
                return;
            }

            $('#k_work').val(weditor.txt.html());
            var fd = $('form').serializeArray();
            var lid = $('#location option:selected').data('id');
            fd.push({name: 'location', value: lid});
            // console.log(fd);
            $.post('/class/add', fd, function (data) {
                if (data.status){
                    alert('SUCCESS: 添加成功');
                    location.reload();
                } else {
                    alert('ERROR: ' + data.msg);
                }
            });
        });

        $('#xg').click(function (e) {
            $('#k_work').val(weditor.txt.html());
            var fd = $('form').serializeArray();
            var lid = $('#location option:selected').data('id');
            fd.push({name: 'location', value: lid});
            $.post('/class/change', fd, function (data) {
                if (data.status){
                    alert('SUCCESS: 修改成功');
                    location.reload();
                } else {
                    alert('ERROR: ' + data.msg);
                }
            });
        });
    });

    var start = {
        format: 'YYYY-MM-DD hh:mm',
        isinitVal: true,
        // onClose: false, 选择后关闭
        zIndex: 20000,
        okfun: function (date) {
            end.minDate = date.val;
        }
    };
    var end = {
        format: 'YYYY-MM-DD hh:mm',
        {% if not cid %}isinitVal: true,{% endif %}
        isTime: false,
        onClose: false,
        {% if not cid %}initDate: [{DD:7},true], {% endif %}
        zIndex: 20000,
        okfun: function (date) {
            var value = date.val;
            value = value.split(' ')[0];
            $(date.elem).val(value + " 12:00");
        }
    };
    $("#start").jeDate(start);
    $("#end").jeDate(end);

    {% if cid > 0 %}
        var d = document.createElement('div');
        $(d).html('{{ clazz.k_work }}');
        console.log($(d).text());
        weditor.txt.html($(d).text());
    {% else %}
        weditor.txt.html('<p>请填写课程作业安排!</p>');
    {% endif %}
</script>